<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>V-model的实现原理及如何定义V-model | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="关于v-model的实现原理以及在组件如何定义v-model是实现双向数据绑定的，可以看作是 value+input的语法糖。那么问题来了 checkbox， select 这些元素也有v-model指令那它又是如何实现的呢?">
<meta property="og:type" content="article">
<meta property="og:title" content="V-model的实现原理及如何定义V-model">
<meta property="og:url" content="http://yoursite.com/2020/06/22/Vue%E4%B8%ADV-model%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%E5%8F%8A%E5%A6%82%E4%BD%95%E5%AE%9A%E4%B9%89V-model/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="关于v-model的实现原理以及在组件如何定义v-model是实现双向数据绑定的，可以看作是 value+input的语法糖。那么问题来了 checkbox， select 这些元素也有v-model指令那它又是如何实现的呢?">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-06-22T10:52:22.000Z">
<meta property="article:modified_time" content="2020-07-30T13:04:01.609Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Vue中V-model的实现原理及如何定义V-model" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/06/22/Vue%E4%B8%ADV-model%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%E5%8F%8A%E5%A6%82%E4%BD%95%E5%AE%9A%E4%B9%89V-model/" class="article-date">
  <time datetime="2020-06-22T10:52:22.000Z" itemprop="datePublished">2020-06-22</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      V-model的实现原理及如何定义V-model
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h4 id="关于v-model的实现原理以及在组件如何定义"><a href="#关于v-model的实现原理以及在组件如何定义" class="headerlink" title="关于v-model的实现原理以及在组件如何定义"></a>关于v-model的实现原理以及在组件如何定义</h4><p>v-model是实现双向数据绑定的，可以看作是 value+input的语法糖。那么问题来了 checkbox， select 这些元素也有v-model指令那它又是如何实现的呢?</p>
<a id="more"></a>
<h5 id="理解"><a href="#理解" class="headerlink" title="理解:"></a>理解:</h5><p>组件的v-model是value+input的语法糖</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">el-checkbox</span> <span class="attr">:value</span> = <span class="string">&quot;&quot;</span> @<span class="attr">input</span>=<span class="string">&quot;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">el-checkbox</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">el-checkbox</span> <span class="attr">v-model</span>=<span class="string">&quot;check&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">el-checkbox</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>全局重新定义组件配置v-model(自定义v-model)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Vue.component(<span class="string">&#x27;el-checkbox&#x27;</span>,&#123;</span><br><span class="line">    template:<span class="string">`&lt;input type = &quot;checkbox&quot; :checked=&quot;check&quot; @change=&quot;$emit(&#x27;change&#x27;,$event.target.checked)&quot;&gt;`</span>,</span><br><span class="line">    model:&#123;</span><br><span class="line">        prop:<span class="string">&#x27;check&#x27;</span>,<span class="comment">//更改默认的value名字</span></span><br><span class="line">        event:<span class="string">&#x27;change&#x27;</span><span class="comment">//更改默认（input）方法名为 change</span></span><br><span class="line">    &#125;,</span><br><span class="line">    props:&#123;</span><br><span class="line">        check:<span class="built_in">Boolean</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h5 id="原理总结"><a href="#原理总结" class="headerlink" title="原理总结"></a>原理总结</h5><ul>
<li>定义组件上的v-model如何不进行配置model默认转化为value+input。</li>
<li>如果是原生v-model，会根据标签的不同生成不同的属性和事件。<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">v-model</span>=<span class="string">&quot;value&quot;</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- value的值为Bealoon类型，监听change方法 --&gt;</span></span><br></pre></td></tr></table></figure>
以上都是根据源码总结的哈</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/06/22/Vue%E4%B8%ADV-model%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%E5%8F%8A%E5%A6%82%E4%BD%95%E5%AE%9A%E4%B9%89V-model/" data-id="ckd8udtbi00069gt4hpyd37a3" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Vue/" rel="tag">Vue</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/06/24/js%E5%9D%97%E7%BA%A7%E4%BD%9C%E7%94%A8%E5%9F%9F%E7%9A%84%E5%9D%91/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          js块级作用域的坑
        
      </div>
    </a>
  
  
    <a href="/2020/06/20/Vue%E4%B8%AD%20v-for%E4%BD%BF%E7%94%A8key%E7%9A%84%E4%BD%9C%E7%94%A8/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Vue中v-for使用key的作用</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vue/" rel="tag">Vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vue%E5%85%A8%E5%AE%B6%E6%A1%B6/" rel="tag">Vue全家桶</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/js/" rel="tag">js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node/" rel="tag">node</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Vue/" style="font-size: 20px;">Vue</a> <a href="/tags/Vue%E5%85%A8%E5%AE%B6%E6%A1%B6/" style="font-size: 10px;">Vue全家桶</a> <a href="/tags/js/" style="font-size: 15px;">js</a> <a href="/tags/node/" style="font-size: 10px;">node</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/07/">July 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">June 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/07/30/hello-world/">我的第一章博客</a>
          </li>
        
          <li>
            <a href="/2020/07/25/%E5%8E%9F%E7%94%9FHttp%E5%AE%9E%E7%8E%B0cookie%E8%AE%BE%E7%BD%AE/">原生Http实现cookie设置</a>
          </li>
        
          <li>
            <a href="/2020/07/23/%E5%B0%81%E8%A3%85Vue%E7%BB%84%E4%BB%B6%E5%B0%86%E4%BB%BB%E6%84%8F%E5%90%8E%E7%BC%80%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%E7%BB%9F%E4%B8%80%E5%85%A8%E9%83%A8%E5%BC%95%E5%85%A5%E5%88%B0%E5%BD%93%E5%89%8D%E6%96%87%E4%BB%B6/">封装Vue组件将任意后缀文件目录统一全部引入到当前文件</a>
          </li>
        
          <li>
            <a href="/2020/07/15/js%E4%B8%AD%E7%9A%84%E9%98%B2%E6%8A%96%E4%B8%8E%E8%8A%82%E6%B5%81/">js中的防抖与节流</a>
          </li>
        
          <li>
            <a href="/2020/07/07/%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0call%EF%BC%8Capply,bind/">手动实现call，apply,bind</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>